﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {

    [SerializeField]
    private Camera mainCamera;
    [SerializeField]
    private float camMoveDuration;

    private int remianTime;
    public bool isPass;
    public GameObject player;
    [SerializeField]
    private GameObject deadline;

    [SerializeField]
    private GameObject bg1;
    [SerializeField]
    private GameObject bg2;

    [SerializeField]
    private GameObject signs;
    [SerializeField]
    private GameObject sign;
    [SerializeField]
    private TextAsset textInfo;

    Queue<GameObject> bgs = new Queue<GameObject>();
    private Vector2 leftBoader;
    private List<Sign> signProperty = new List<Sign>();

    // Use this for initialization
    Transform[] signTrans;
    void Start () {
        isPass = false;
        signTrans = signs.GetComponentsInChildren<Transform>();
        InitObs();
        InitBG();
	}
	
	// Update is called once per frame
	void Update () {
        if (isPass)
        {
            Debug.Log("恭喜过关");
            Time.timeScale = 0;
        }
        deadline.transform.position = new Vector2(player.transform.position.x, -10f);
        BackgroundCycle();
	}

    private void LateUpdate()
    {
        CameraFollow(camMoveDuration);
        leftBoader = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
        if (player.transform.position.x < leftBoader.x)
        {
            player.transform.position = new Vector2(leftBoader.x, player.transform.position.y);
        }
    }

    void InitObs()
    {
        string[] texts = textInfo.text.Split('\n');
        for (int i = 1; i < signTrans.Length; i++)
        {
            GameObject ob = Instantiate(sign, signTrans[i].position, Quaternion.identity);
            Sign _sign = ob.AddComponent<Sign>();
            _sign.index = i;
            if(i < texts.Length)
            {
                _sign.content = texts[i];
            }
            else
            {
                _sign.content = "测试" + i;
            }
            signProperty.Add(_sign);
        }
    }

    public void setIsPass(bool isPass)
    {
        this.isPass = isPass;
    }

    private void InitBG()
    {
        bgs.Enqueue(bg1);
        bgs.Enqueue(bg2);
    }

    private void CameraFollow(float duration)
    {
        float px = player.transform.position.x;
        float py = player.transform.position.y;
        float x = Mathf.Clamp(px, mainCamera.transform.position.x, px);
        float y = Mathf.Clamp(py, 0, 8f);
        mainCamera.transform.position = new Vector3(Mathf.Lerp(mainCamera.transform.position.x, x, duration), Mathf.Lerp(mainCamera.transform.position.y, y, duration), -10);
    }

    void BackgroundCycle()
    {
        if (mainCamera.transform.position.x >= bgs.Peek().transform.position.x + 47f)
        {
            GameObject next = bgs.Dequeue();
            next.transform.position = new Vector2(next.transform.position.x + 47f * 2, next.transform.position.y);
            bgs.Enqueue(next);
        }
    }
}
